package cn.hr.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hr.dao.mapper.SysRoleMapper;
import cn.hr.dao.mapper.SysUserRoleMapper;
import cn.hr.pojo.SysRole;
import cn.hr.pojo.SysUserRole;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.hr.dao.mapper.SysUserMapper;
import cn.hr.pojo.SysUser;
import cn.hr.service.SysUserService;

/**
 * 用户服务接口实现类
 *
 * @author MO
 */
@CacheConfig(cacheNames = "user")
@Scope("prototype")
@Service("userService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysUserRoleMapper userRoleMapper;

    @Override
    public boolean insert(SysUser t) throws Exception {
        boolean result = false;
        t.setCode("USER_CODE"+(userMapper.findMaxId()+1));
        SysUserRole sysUserRole = new SysUserRole();//用户角色
        sysUserRole.setCreatedById(t.getCreatedById());
        sysUserRole.setCreatedDate(new Date());
        sysUserRole.setRoleId(t.getRoleId());//创建的用户角色类别
        if (userMapper.insert(t)>0){
            t = userMapper.getByCode(t.getCode());//获取userid
            sysUserRole.setUserId(t.getId());
            if (userRoleMapper.insert(sysUserRole)>0){
                result = true;
            }
        }
        return result;
    }

    @Override
    public boolean update(SysUser t) throws Exception {
        boolean result = false;
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setModifyDate(new Date());
        sysUserRole.setModifyById(t.getCreatedById());
        sysUserRole.setRoleId(t.getRoleId());
        sysUserRole.setUserId(t.getId());
        if (userRoleMapper.update(sysUserRole)>0){
            if (userMapper.update(t)>0) {
                result = true;
            }
        }
        return  result;
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        boolean result = false;
        if (userRoleMapper.delete(id)>0){
            if (userMapper.delete(id)>0){
                result = true;
            }
        }
        return  result;
    }

    @Override
    public SysUser get(Integer id) throws Exception {
        return userMapper.get(id);
    }

    @Override
    public PageInfo<Map> findListByCondition(Map map, int pageNo) throws Exception {
        PageHelper.startPage(pageNo,Constants.PAGESIZE);
        List<Map> list = userMapper.findListByCondition(map);
        PageInfo<Map>  pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Long findCountByCondition(Map m) throws Exception {
        return null;
    }

    @Override
    public SysUser getByCode(String code) throws Exception {
        return userMapper.getByCode(code);
    }

    /**
     * 根据用户id获取角色id
     * @param userId
     * @return
     * @throws Exception
     */
    @Cacheable(keyGenerator = "keyGenerator")
    public Integer getRoleIdByUserId(Integer userId) throws Exception{
        return userRoleMapper.getRoleIdByUserId(userId);
    }

}
